<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['production:rzm:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      height="calc(100% - 150px)"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="rzmList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="生产日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rzmRq"
        width="90"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rzmRq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="机台编号"
        :show-overflow-tooltip="true"
        width="75"
        align="center"
        prop="rzmJtbh"
      />
      <el-table-column
        label="指令代码"
        :show-overflow-tooltip="true"
        width="75"
        align="center"
        prop="rzmZldm"
      />
      <el-table-column
        label="指令名称"
        :show-overflow-tooltip="true"
        width="75"
        align="center"
        prop="rzmZlmc"
      />
      <el-table-column
        label="良品数"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="rzmAsl"
      />
      <el-table-column
        label="不良数"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="rzmBsl"
      />
      <el-table-column
        label="采集时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="rzmJlrq"
        width="120"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.rzmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="成型时间"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="rzmCxsj"
      />
      <el-table-column
        label="实际成型时间"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="rzmSjcxsj"
      />
      <el-table-column
        label="工号"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="rzmWkno"
      />
      <el-table-column
        label="操作员"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="rzmName"
      />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeZzdh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeSodh"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        min-width="250px"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="计划数量"
        :show-overflow-tooltip="true"
        width="75"
        align="center"
        prop="moeScsl"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeMjbh"
      />
      <el-table-column
        label="模具名称"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeMjmc"
      />
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="850px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-row>
          <el-col :span="14">
            <el-form-item label="生产日期">
              <el-date-picker
                v-model="daterangeRzmRq"
                size="small"
                style="width: 333px"
                type="datetimerange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="制造单号" prop="moeZzdh">
              <el-input
                v-model="queryParams.moeZzdh"
                placeholder="请输入制造单号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="销售单号" prop="moeSodh">
              <el-input
                v-model="queryParams.moeSodh"
                placeholder="请输入销售单号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="物料代码" prop="moeWldm">
              <el-input
                v-model="queryParams.moeWldm"
                placeholder="请输入物料代码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="机台编号" prop="rzmJtbh">
              <el-input
                v-model="queryParams.rzmJtbh"
                placeholder="请输入机台编号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="指令代码" prop="rzmZldm">
              <el-input
                v-model="queryParams.rzmZldm"
                placeholder="请输入指令代码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              style="margin-top: 25px; display: flex; justify-content: flex-end"
            >
              <el-button @click="cancel">取消</el-button>
              <el-button type="primary" @click="handleQuery">确认</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 添加或修改数据采集日志查询对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      ></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRzm,
  getRzm,
  delRzm,
  addRzm,
  updateRzm,
  exportRzm,
} from "@/api/production/rzm";

export default {
  name: "Rzm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 数据采集日志查询表格数据
      rzmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期时间范围
      daterangeRzmRq: [this.$moment().add(-6, "hours"), this.$moment()],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        rzmRq: null,
        rzmJtbh: null,
        rzmZldm: null,
        moeZzdh: null,
        moeSodh: null,
        moeWldm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据采集日志查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRzmRq && "" != this.daterangeRzmRq) {
        this.queryParams.params["beginRzmRq"] = this.moment(
          this.daterangeRzmRq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endRzmRq"] = this.moment(
          this.daterangeRzmRq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listRzm(this.queryParams).then((response) => {
        this.rzmList = [];
        this.rzmList = this.rzmList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listRzm(this.queryParams).then((response) => {
          this.rzmList = this.rzmList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rzmId: null,
        rzmRq: null,
        rzmBcdm: null,
        rzmJtbh: null,
        rzmZldm: null,
        rzmCount: null,
        rzmXs: null,
        rzmCxsj: null,
        rzmSl: null,
        rzmMoeid: null,
        rzmWkno: null,
        rzmPreksrq: null,
        rzmJlrq: null,
        rzmCjrq: null,
        rzmRymid: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.rzmList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRzmRq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.rzmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据采集日志查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rzmId = row.rzmId || this.ids;
      getRzm(rzmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据采集日志查询";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询数据采集日志查询";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rzmId != null) {
            updateRzm(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRzm(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rzmIds = row.rzmId || this.ids;
      this.$confirm(
        '是否确认删除数据采集日志查询编号为"' + rzmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRzm(rzmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据采集日志查询数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportRzm(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
